﻿<div [@routerTransition]>
    <div class="m-subheader">
        <div class="row align-items-center">
            <div class="mr-auto col-auto">
                <h3 class="m-subheader__title m-subheader__title--separator">
                    <span>{{"Users" | localize}}</span>
                </h3>
                <span class="m-section__sub">
                    {{"UsersHeaderInfo" | localize}}
                </span>
            </div>
            <div class="col text-right mt-3 mt-md-0">
                <div class="btn-group dropdown mr-2" dropdown>
                    <button dropdownToggle class="dropdown-toggle btn btn-outline-success">
                        <i class="far fa-file-excel"></i><span class="caret"></span> {{"ExcelOperations" | localize}}
                    </button>
                    <ul class="dropdown-menu dropdown-menu-right dropdown-excel-operations" *dropdownMenu>
                        <li>
                            <a href="javascript:;"
                               (click)="exportToExcel()">
                                <i class="fa fa-download"></i>
                                {{'ExportToExcel' | localize}}
                            </a>
                        </li>

                        <li>
                            <a href="javascript:;" *ngIf="isGranted('Pages.Administration.Users.Create')" class="no-padding">
                                <span>
                                    <p-fileUpload customUpload="true"
                                                  name="ExcelFileUpload"
                                                  #ExcelFileUpload
                                                  maxFileSize="10000000"
                                                  auto="auto"
                                                  accept=".csv,.xls,.xlsx"
                                                  (uploadHandler)="uploadExcel($event)"
                                                  (onError)="onUploadExcelError()"
                                                  chooseLabel="{{'ImportFromExcel' | localize}}">
                                    </p-fileUpload>
                                </span>
                            </a>
                        </li>
                        <li class="dropdown-divider"></li>
                        <li>
                            <span class="dropdown-item-text text-muted" href="#">
                                <small [innerHTML]="l('ImportToExcelSampleFileDownloadInfo', '<a href=/assets/sampleFiles/ImportUsersSampleFile.xlsx>' + l('ClickHere') + '</a>')"></small>
                            </span>
                        </li>

                    </ul>
                </div>
                <button (click)="createUser()" *ngIf="isGranted('Pages.Administration.Users.Create')" class="btn btn-primary"><i class="fa fa-plus"></i> {{"CreateNewUser" | localize}}</button>
            </div>
        </div>
    </div>
    <div class="m-content">
        <div class="m-portlet m-portlet--mobile">
            <div class="m-portlet__body">
                <form class="horizontal-form" autocomplete="off">
                    <div class="m-form m-form--label-align-right">
                        <div class="row align-items-center m--margin-bottom-10">
                            <div class="col-xl-12">
                                <div class="form-group m-form__group align-items-center">
                                    <div class="input-group">
                                        <input [(ngModel)]="filterText" name="filterText" autoFocus class="form-control m-input" [placeholder]="l('SearchWithThreeDot')" type="text">
                                        <span class="input-group-btn">
                                            <button (click)="getUsers()" class="btn btn-primary" type="submit"><i class="flaticon-search-1" [attr.aria-label]="l('Search')"></i></button>
                                        </span>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="row m--margin-bottom-10" [hidden]="!advancedFiltersAreShown">
                            <div [ngClass]="{'col-md-6': ('Pages.Administration.Roles' | permission), 'col-md-12':!isGranted('Pages.Administration.Roles')}">
                                <div class="form-group">
                                    <permission-combo name="permissionFilter" [(ngModel)]="selectedPermission"></permission-combo>
                                </div>
                            </div>
                            <div class="col-md-6" *ngIf="'Pages.Administration.Roles' | permission">
                                <div class="form-group">
                                    <role-combo name="roleFilter" [(ngModel)]="role"></role-combo>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="m-checkbox-list">
                                    <label for="UsersTable_OnlyLockedUsers" class="m-checkbox">
                                        <input id="UsersTable_OnlyLockedUsers" type="checkbox" name="OnlyLockedUsers" [(ngModel)]="onlyLockedUsers">
                                        {{"OnlyLockedUsers" | localize}}
                                        <span></span>
                                    </label>
                                </div>
                            </div>
                        </div>

                        <div class="row margin-bottom-10" *ngIf="advancedFiltersAreShown">
                            <div class="col-sm-12 text-right">
                                <button class="btn btn-metal" (click)="getUsers()"><i class="fa fa-sync"></i> {{"Refresh" | localize}}</button>
                            </div>
                        </div>
                        <div class="row margin-bottom-10">
                            <div class="col-sm-12">
                                <span class="clickable-item text-muted" *ngIf="!advancedFiltersAreShown" (click)="advancedFiltersAreShown=!advancedFiltersAreShown"><i class="fa fa-angle-down"></i> {{"ShowAdvancedFilters" | localize}}</span>
                                <span class="clickable-item text-muted" *ngIf="advancedFiltersAreShown" (click)="advancedFiltersAreShown=!advancedFiltersAreShown"><i class="fa fa-angle-up"></i> {{"HideAdvancedFilters" | localize}}</span>
                            </div>
                        </div>
                    </div>
                </form>

                <div class="row align-items-center">
                    <!--<Primeng-TurboTable-Start>-->
                    <div class="primeng-datatable-container"
                         [busyIf]="primengTableHelper.isLoading">
                        <p-table #dataTable
                                 (onLazyLoad)="getUsers($event)"
                                 [value]="primengTableHelper.records"
                                 rows="{{primengTableHelper.defaultRecordsCountPerPage}}"
                                 [paginator]="false"
                                 [lazy]="true"
                                 [scrollable]="true"
                                 ScrollWidth="100%"
                                 [responsive]="primengTableHelper.isResponsive"
                                 [resizableColumns]="primengTableHelper.resizableColumns">
                            <ng-template pTemplate="header">
                                <tr>
                                    <th style="width: 130px" [hidden]="!isGrantedAny('Pages.Administration.Users.Impersonation', 'Pages.Administration.Users.Edit', 'Pages.Administration.Users.ChangePermissions', 'Pages.Administration.Users.Delete')">{{'Actions' | localize}}</th>
                                    <th style="width: 150px" pSortableColumn="username">
                                        {{'UserName' | localize}}
                                        <p-sortIcon field="username"></p-sortIcon>
                                    </th>
                                    <th style="width: 150px" pSortableColumn="name">
                                            姓名
                                        <p-sortIcon field="name"></p-sortIcon>
                                    </th>
                                    <th style="width: 150px">
                                        {{'Roles' | localize}}
                                    </th>
                                    <th style="width: 250px" pSortableColumn="emailAddress">
                                        {{'EmailAddress' | localize}}
                                        <p-sortIcon field="emailAddress"></p-sortIcon>
                                    </th>
                                    <th style="width: 200px" pSortableColumn="isEmailConfirmed">
                                        {{'EmailConfirm' | localize}}
                                        <p-sortIcon field="isEmailConfirmed"></p-sortIcon>
                                    </th>
                                    <th style="width: 100px" pSortableColumn="isActive">
                                        {{'Active' | localize}}
                                        <p-sortIcon field="isActive"></p-sortIcon>
                                    </th>
                                    <th style="width: 200px" pSortableColumn="creationTime">
                                        {{'CreationTime' | localize}}
                                        <p-sortIcon field="creationTime"></p-sortIcon>
                                    </th>
                                </tr>
                            </ng-template>
                            <ng-template pTemplate="body" let-record="$implicit">
                                <tr>
                                    <td style="width: 130px"
                                        [hidden]="!isGrantedAny('Pages.Administration.Users.Impersonation', 'Pages.Administration.Users.Edit', 'Pages.Administration.Users.ChangePermissions', 'Pages.Administration.Users.Delete')">
                                        <div class="btn-group dropdown" dropdown container="body">
                                            <button dropdownToggle class="dropdown-toggle btn btn-sm btn-primary">
                                                <i class="fa fa-cog"></i><span class="caret"></span> {{"Actions" | localize}}
                                            </button>
                                            <ul class="dropdown-menu" *dropdownMenu>
                                                <li>
                                                    <a href="javascript:;" *ngIf="('Pages.Administration.Users.Impersonation' | permission) && record.id !== appSession.userId"
                                                       (click)="_impersonationService.impersonate(record.id, appSession.tenantId)">{{'LoginAsThisUser' | localize}}</a>
                                                </li>
                                                <li>
                                                    <a href="javascript:;" *ngIf="'Pages.Administration.Users.Edit' | permission"
                                                       (click)="createOrEditUserModal.show(record.id)">{{'Edit' | localize}}</a>
                                                </li>
                                                <li>
                                                    <a href="javascript:;" *ngIf="'Pages.Administration.Users.ChangePermissions' | permission"
                                                       (click)="editUserPermissionsModal.show(record.id, record.userName)">{{'Permissions' | localize}}</a>
                                                </li>
                                                <li>
                                                    <a href="javascript:;" *ngIf="'Pages.Administration.Users.ChangePermissions' | permission"
                                                       (click)="unlockUser(record)">{{'Unlock' | localize}}</a>
                                                </li>
                                                <li>
                                                    <a href="javascript:;" *ngIf="'Pages.Administration.Users.Delete' | permission"
                                                       (click)="deleteUser(record)">{{'Delete' | localize}}</a>
                                                </li>
                                            </ul>
                                        </div>
                                    </td>
                                    <td style="width: 150px">
                                        <span class="ui-column-title"> {{'UserName' | localize}}</span>
                                        {{record.userName}}
                                    </td>
                                    <td style="width: 150px">
                                        <span class="ui-column-title"> {{'Name' | localize}}</span>
                                        {{record.surname}} {{record.name}}
                                    </td>
                                    <td style="width: 150px">
                                        <span class="ui-column-title"> {{'Roles' | localize}}</span>
                                        {{getRolesAsString(record.roles)}}
                                    </td>
                                    <td style="width: 250px">
                                        <span class="ui-column-title"> {{'EmailAddress' | localize}}</span>
                                        {{record.emailAddress}}
                                    </td>
                                    <td style="width: 200px">
                                        <span class="ui-column-title"> {{'EmailConfirm' | localize}}</span>
                                        <span class="label label-success" *ngIf="record.isEmailConfirmed">{{'Yes' | localize}}</span>
                                        <span class="label label-default" *ngIf="!record.isEmailConfirmed">{{'No' | localize}}</span>
                                    </td>
                                    <td style="width: 100px">
                                        <span class="ui-column-title"> {{'Active' | localize}}</span>
                                        <span class="label label-success" *ngIf="record.isActive">{{'Yes' | localize}}</span>
                                        <span class="label label-default" *ngIf="!record.isActive">{{'No' | localize}}</span>
                                    </td>
                                    <td style="width: 200px">
                                        <span class="ui-column-title"> {{'CreationTime' | localize}}</span>
                                        {{record.creationTime | momentFormat:'L'}}
                                    </td>
                                </tr>
                            </ng-template>
                        </p-table>
                        <div class="primeng-no-data" *ngIf="primengTableHelper.totalRecordsCount == 0">
                            {{'NoData' | localize}}
                        </div>
                        <div class="primeng-paging-container">
                            <p-paginator [rows]="primengTableHelper.defaultRecordsCountPerPage"
                                         #paginator
                                         (onPageChange)="getUsers($event)"
                                         [totalRecords]="primengTableHelper.totalRecordsCount"
                                         [rowsPerPageOptions]="primengTableHelper.predefinedRecordsCountPerPage">

                            </p-paginator>
                            <span class="total-records-count">
                                {{'TotalRecordsCount' | localize:primengTableHelper.totalRecordsCount}}
                            </span>
                        </div>
                    </div>
                    <!--<Primeng-TurboTable-End>-->
                </div>
            </div>
        </div>
    </div>

    <createOrEditUserModal #createOrEditUserModal (modalSave)="getUsers()"></createOrEditUserModal>
    <editUserPermissionsModal #editUserPermissionsModal></editUserPermissionsModal>
</div>
